#!/usr/bin/ksh
set -x

export START_DATE=""
export END_DATE=""
export TAG=""

function Usage
{
  echo wrong usage;
}

function parseCommandLine
{
  #set -x
  while getopts s:e:t: name
  do
  #echo in while
      case $name in
      s) START_DATE="$OPTARG";;
      e) END_DATE="$OPTARG";;
      t) TAG="$OPTARG";;
      ?) Usage $0
         exit 2;;
      esac
  done
  #echo out parseCommandLine
}

function main
{
  set -x
  parseCommandLine $@

  #echo "the tag is $TAG <<<"
  if [[ $START_DATE"ZZ" != "ZZ" ]]
  then
      START_OPT="-s ${START_DATE}"
  fi

  if [[ $END_DATE"ZZ" != "ZZ" ]]
  then
      END_OPT="-e ${END_DATE}"
  fi
  if [[ $TAG"ZZ" == "ZZ" ]]
  then
      echo "$0: tag not specified"
      exit 2;
  fi

  #echo p2search -p"'\[${TAG} *=[0-9]*\]'" ${START_OPT}  ${END_OPT}
  #p2search -p"\[${TAG} *=[0-9]*\]" ${START_OPT}  ${END_OPT}
  echo p2search -p"\[${TAG} *=[0-9]*\]" ${START_OPT}  ${END_OPT} | egrep -e "\[${TAG} *=[0-9]*\]" | sed -e"s/^.*\[${TAG}=\([0-9]*\)\].*$/\1+/" | paste -s | sed -e 's/+$//' | bc
  p2search -p"\[${TAG} *=[0-9]*\]" ${START_OPT}  ${END_OPT} | egrep -e "\[${TAG} *=[0-9]*\]" | sed -e"s/^.*\[${TAG}=\([0-9]*\)\].*$/\1+/" | paste -s | sed -e 's/+$//' | bc
}


main $@
